package com.mall.shopping.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Value;

//import javax.servlet.ServletInputStream;
//import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 *
 *
 */
public class ProjectUtils {

    // 商品搜索的相似度，从配置文件中赋值
    @Value("${searchGoods.goodsSimilarity}")
    public static Double goodsSimilarity;

//    // 需要拦截的前台接口
//    public static String filterInterface;
//    // 商品搜索相似度
//    public static Double goodsSimlarity;
//    static {
//        Properties properties = new Properties();
//        try {
//            InputStream resourceAsStream = FileUploadUtils.class.getClassLoader().getResourceAsStream("mall.properties ");
//            properties.load(resourceAsStream);
//            filterInterface = properties.getProperty("filterInterface");
//            goodsSimlarity = Double.parseDouble(properties.getProperty("goodsSimlarity"));
//            resourceAsStream.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    // 获取请求体中的参数
//    public static String getParamInRequestBody(HttpServletRequest request) throws IOException {
//        ServletInputStream inputStream = request.getInputStream();
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        byte[] bytes = new byte[1024];
//        int length;
//        while ((length = inputStream.read(bytes)) != -1){
//            byteArrayOutputStream.write(bytes, 0, length);
//        }
//        String requestInfo = byteArrayOutputStream.toString("utf-8");
//        byteArrayOutputStream.close();
//        return requestInfo;
//    }


    /***
     * *以最短的字符串的长度作为分母, 以两个字符串中相同的字符个数(不重复)作为分子,计算相 似度
     * @param strOne
     * @param strTwo
     * @return 两字符串相似度(最小为0,最大为1)
     * */
    public static double SimlarityString(String strOne, String strTwo) {
        Set<String> seta = new HashSet<String>();
        Set<String> setb = new HashSet<String>();
        for (int i = 0; i < strOne.length(); i++) {
            seta.add(strOne.substring(i, i + 1));
        }
        for (int i = 0; i < strTwo.length(); i++) {
            setb.add(strTwo.substring(i, i + 1));
        }
        double x = 0;
        double y = 0;
        if (seta.size() != 0 && setb.size() != 0) {
            if (seta.size() >= setb.size()) {
                y = setb.size();
            } else {
                y = seta.size();
            }
            for (Object obja : seta) {
                for (Object objb : setb) {
                    if (obja.equals(objb)) {
                        x++;
                    }
                }
            }
            double z = 0.0;
            try {
                z = x / y;
            } catch (Exception e) { }
            return z;
        } else {
            return 0;
        }
    }
    /**
     *
     * 获取汉字串拼音，英文字符不变
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String cn2Spell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    if (arr[i] == 65288 || arr[i] == 65289)
                        continue;
                    pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat) [0]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString();
    }
}
